<?php
include('mysqli_connect.php');
$params = json_decode(file_get_contents("php://input"), true);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
	$username = $params['username'];
	$password = $params['password'];
}
$userSql = "select * from user where user_name='{$username}' and user_password='{$password}'";
$userRes = mysqli_query($dbc, $userSql);
if (!$userRes) {
	printf("Error: %s\n", mysqli_error($dbc));
	exit();
}
$result2 = mysqli_fetch_array($userRes);
if (mysqli_num_rows($userRes) == 1) {
	//获得用户id
	$idSql =	"select user_id from user where user_name='{$username}'";
	$result = mysqli_query($dbc, $idSql);
	if ($result) {
		// 查询成功
		$row = mysqli_fetch_assoc($result);
		$id = $row['user_id'];
		mysqli_free_result($result); // 释放结果集
		$response = array('status' => true, 'message' => '登陆成功', 'id' => $id);
	} else {
		$response = array('status' => false, 'message' => '登录失败', 'id' => -1);
	}
	session_start();
	$_SESSION['userId'] = $id;
	echo json_encode($response);
} else {
	$response = array('status' => false, 'message' => '账号或密码错误');
	echo json_encode($response);
}
